<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Exception;
use think\response\Json;
use think\Session;

/**
 * 订单管理
 */

class Ord extends AdminBase
{
    /**
     * 首页展示
     * @param string $nickname
     * @param string $orderId
     * @param string $ostatus
     * @param int $page
     * @return mixed
     */
    public function index($nickname = '', $orderId = '', $ostatus = 0, $page = 1)
    {

        $where['a.status'] = $ostatus;

        if($nickname){
            $where['d.nickname'] = ['like', "%{$nickname}%"];
        }

        if($orderId){
            $where['a.order_id'] = $orderId;
        }

        $field = "a.id,a.order_id,a.payways,a.source_order_id,a.pay_method,
        a.status,a.created_at,a.price,c.address,c.name,c.phone,c.province,c.city,c.area,
        a.remark,d.nickname,d.headimgurl,e.goods_name,e.goods_thumb,e.goods_spec,e.number,f.courier_company,f.courier_code,f.courier_id";

        $ord_list = Db::table('lsj_order')->alias('a')
            ->join('lsj_user d','a.uid = d.uid','LEFT')
            ->join('lsj_order_ship_address c','a.order_id=c.order_id','LEFT')
            ->join('lsj_inquiry_sheet e','a.inquiry_sheet_id = e.id','left')
            ->join('lsj_order_courier f','a.order_id=f.order_id','left')
            ->field($field)
            ->where($where)->order('a.created_at desc')->paginate(10, false, ['page' => $page]);
        $courier_info = Db::table('lsj_zd_courier')->select();
        return $this->fetch('index',[
            'ord_list'=>$ord_list,
            'nickname'=>$nickname,
            'order_id'=>$orderId,
            'order_status'=>$ostatus,
            'courier_list' => $courier_info
        ]);
    }

    /**
     * 填写快递
     */
    public function create_courier()
    {
        $data = $this->request->only([
            'order_id',
            'courier_company',
            'courier_code',
            'courier_id'
        ]);

        $order_id = $data['order_id'];
        $order_info = Db::table('lsj_order')->where("order_id = '$order_id' and status=1")->find();
        if(!$order_info){
            return new Json([
               'code' => 0,
               'message' => '订单不是已付款状态'
            ]);
        }

        Db::startTrans();
        try{
            //插入订单
            $data['courier_info'] = '';
            Db::table('lsj_order_courier')->insert($data);
            //更改订单状态
            Db::table('lsj_order')->where("id={$order_info['id']}")->update(['status'=>2]);
            //插入订单操作
            Db::table('lsj_order_detail')->insert([
                'order_id' => $data['order_id'],
                'remark' => Session::get('admin_nickname')."发货",
                'status' => 2,
                'created_at' => date('Y-m-d H:i:s',time())
            ]);
            Db::commit();
            return new Json([
                'code' => 1,
                'message' => '填写成功'
            ]);
        }catch (Exception $e){
            Db::rollback();
            return new Json([
                'code' => 0,
                'message' => $e->getMessage()
            ]);
        }
    }

    /**
     * 修改物流
     */
    public function update_courier()
    {
        $data = $this->request->only([
            'order_id',
            'courier_company',
            'courier_code',
            'courier_id'
        ]);

        $order_id = $data['order_id'];
        unset($data['order_id']);
        $order_info = Db::table('lsj_order')->where("order_id = '$order_id' and status=2")->find();
        if(!$order_info){
            return new Json([
                'code' => 0,
                'message' => '订单不是代发货状态'
            ]);
        }


        try{
            //插入订单
            Db::table('lsj_order_courier')->where("order_id='$order_id'")->update($data);
            return new Json([
                'code' => 1,
                'message' => '填写成功'
            ]);
        }catch (Exception $e){
            return new Json([
                'code' => 0,
                'message' => $e->getMessage()
            ]);
        }
    }
}